package com.example.springboot.utils;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public class EmailUtils {
    @Value("${spring.mail.username}")
    private String username; // 发件人邮箱地址
    @Value("${spring.mail.password}")
    private String password; // 如果是qq邮箱可以使户端授权码

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 发送邮件
     * @param email 收件人邮箱
     */

    public String sendMail(String email){
        try {
            LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100,4,20);
            // 图形验证码写出到指定文件
            lineCaptcha.write("D:/test/captcha.png");
            // 4.保存验证码到 session
            String code = lineCaptcha.getCode();
           // stringRedisTemplate.opsForValue().set("code"+email, code, 5L, TimeUnit.MINUTES);
            // 将验证码存入redis缓存
            stringRedisTemplate.opsForValue().set(email, code, 5, TimeUnit.MINUTES);
            // 存一个验证码发送时间，1分钟之内不能连续发送
            stringRedisTemplate.opsForValue().set(email + "time", "time", 1, TimeUnit.MINUTES);
            // 5.发送验证码
            log.debug("发送短信验证码成功，验证码：{}", code);
            final Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", "smtp.qq.com");

            // 发件人的账号
            props.put("mail.user", username);
            //发件人的密码
            props.put("mail.password", password);

            // 构建授权信息，用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用环境属性和授权信息，创建邮件会话
            Session mailSession = Session.getInstance(props, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            String username = props.getProperty("mail.user");
            InternetAddress form = new InternetAddress(username);
            message.setFrom(form);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(email);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件标题
            message.setSubject("注册验证码,有效时间为5分钟");

            // 设置邮件的内容体
            message.setContent(code, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
            return code;
        }catch (Exception e){
            e.printStackTrace();
        }
        return "false";
    }

}
